<template>
  <div>
    <el-table :data="records" style="width: 100%" border>
      <el-table-column type="index" label="序号" width="80" align="center"> </el-table-column>
      <el-table-column prop="skuName" label="名称" width="width"> </el-table-column>
      <el-table-column prop="skuDesc" label="描述" width="width"> </el-table-column>
      <el-table-column label="默认图片" width="110">
        <template slot-scope="{row, $index}">
          <img :src="row.skuDefaultImg" alt="" style="width: 80px; height: 80px">
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量" width="80"> </el-table-column>
      <el-table-column prop="price" label="价格" width="80"> </el-table-column>
      <el-table-column prop="prop" label="操作" width="width">
        <template slot-scope="{row, $index}">
          <el-button type="success" icon="el-icon-sort-up" size="mini" v-if="row.isSale == 0" @click="onSale(row)" title="上架"></el-button>
          <el-button type="warning" icon="el-icon-sort-down" size="mini" v-if="row.isSale == 1" @click="cancel(row)" title="下架"></el-button>
          <el-button type="primary" icon="el-icon-edit" size="mini" title="编辑" @click="edit"></el-button>
          <el-button type="info" icon="el-icon-info" size="mini" title="更多" @click="getSkuInfo(row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" title="删除"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @current-change="getSkuList"
      @size-change="handleSizeChange"
      :current-page="1"
      :page-sizes="[3, 5, 10]"
      :page-size="10"
      layout="prev, pager, next, jumper, ->, total, sizes"
      :total="20"
      style="text-align: center">
    </el-pagination>

    <el-drawer :visible.sync="drawer" size="50%" :with-header="false">
      <div class="drawer-body">
        <el-row>
          <el-col :span="5">名称</el-col>
          <el-col :span="16">{{skuInfo.skuName}}</el-col>
        </el-row>
        <el-row>
          <el-col :span="5">描述</el-col>
          <el-col :span="16">{{skuInfo.skuDesc}}</el-col>
        </el-row>
        <el-row>
          <el-col :span="5">价格</el-col>
          <el-col :span="16">{{skuInfo.price}}元</el-col>
        </el-row>
        <el-row>
          <el-col :span="5">平台属性</el-col>
          <el-col :span="16">
            <template>
              <el-tag type="success" v-for="attr in skuInfo.skuAttrValueList" :key="attr.id" style="margin-right: 10px"> {{attr.valueName}} </el-tag>
            </template>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="5">商品图片</el-col>
          <el-col :span="16">
            <el-carousel height="500px">
              <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
                <img :src="item.imgUrl" style="width: 100%; height: 100%">
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </div>
    </el-drawer>

  </div>
</template>

<script>
export default {
  name: 'Sku',
  data() {
    return {
      page: 1, //当前页数
      limit: 10, //当前页面的数据条数
      total: '', //分页器一共展示的数据条数
      records: [], //SKU列表的数据
      skuInfo: {}, //存储SKU信息
      drawer: false //抽屉默认为关闭状态
    }
  },
  mounted() {
    this.getSkuList()
  },
  methods: {
    // 获取SKU列表数据
    async getSkuList(pages = 1) {
      this.page = pages
      const {page, limit} = this
      let result = await this.$API.sku.reqSkuList(page, limit)
      if(result.code == 200) {
        this.total = result.data.total
        this.records = result.data.records
      }
    },
    // 分页器每页显示数据条数的回调
    handleSizeChange(limits) {
      this.limit = limits
      this.getSkuList()
    },
    // 设置销售状态为上架
    async onSale(row) {
      let result =  await this.$API.sku.reqCancel(row.id)
      if(result.code == 200) {
        this.$message({type: 'success', message: '上架成功'})
        row.isSale = 1
      }
    },
    // 设置销售状态为下架
    async cancel(row) {
      let result =  await this.$API.sku.reqSale(row.id)
      if(result.code == 200) {
        this.$message({type: 'success', message: '下架成功'})
        row.isSale = 0
      }
    },
    // 编辑按钮的回调
    edit() {
      this.$message({type: 'info', message: '正在开发中'})
    },
    // 获取SKU信息
   async getSkuInfo(sku) {
    // 打开抽屉
     this.drawer = true
     let result = await this.$API.sku.reqSkuById(sku.id) 
     if(result.code == 200) {
      this.skuInfo = result.data
     }
    }
  }
}
</script>

<style>

</style>

<style scoped>
.drawer-body {
  padding: 30px 0 0 80px;
}
.el-row .el-col-5{
  font-size: 28px;
}
.el-col {
  margin: 10px 10px;
}
>>>.el-carousel__button {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background-color: red;
}
.el-carousel__item:nth-child(2n) {
   background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n+1) {
   background-color: #d3dce6;
}
</style>
